/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

import Qt 4.7
import com.nokia.meego 1.0

import "components"
import "OAuth"
import "js/Core.js" as Core

Page {
    id: root

    tools: ToolBarLayout {
        ToolIcon {
            iconId: "toolbar-back"
            onClicked: {
                globalSettings.oauthToken = "";
                globalSettings.oauthSecret = "";
                pageStack.pop()
            }
        }
    }

    PageBackground { }

    PageHeader {
        id: pageHeader
        text: qsTr("xREL OAuth")
    }

    OAuth{
        id: oauth
        anchors.top: pageHeader.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        username: usernameInput.inputText
        password: passwordInput.inputText
        externalBrowser: externalSwitch.checked
        automatedLogin: appLogin.checked

        onAuthenticationCompleted: {
            Core.saveCredentials(accessToken, accessSecret);
            globalSettings.oauthToken = accessToken;
            globalSettings.oauthSecret = accessSecret;
            pageStack.pop();
        }
        onAuthenticationFailed: {
            globalSettings.oauthToken = "";
            globalSettings.oauthSecret = "";
            controlFlickable.visible = true;
            failedDialog.errorMsg = errorMsg;
            failedDialog.open();
        }
    }

    QueryDialog {
        id: failedDialog
        property string errorMsg
        titleText: qsTr("OAuth failed!")
        message: qsTr("The OAuth process has failed. Error:<br>") + errorMsg
        acceptButtonText: qsTr("Ok")
    }

    Flickable {
        id: controlFlickable
        anchors.top: pageHeader.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        contentHeight: childrenRect.height
        clip: true

        Column {
            id: contentColumn
            anchors.fill: parent
            spacing: 0

            SectionEntrySwitch {
                id: externalSwitch
                titleText: qsTr("Use external browser")
                onClicked: if(!checked) appLogin.checked = false
            }

            SectionEntrySwitch {
                id: appLogin
                titleText: qsTr("Allow direct login")
                checked: true
                opacity: !externalSwitch.checked
            }

            Item {
                id: infoItem
                width: parent.width - 12
                height: childrenRect.height + 12
                opacity: appLogin.checked

                Label {
                    id: infoLabel
                    anchors.horizontalCenter: parent.horizontalCenter
                    width: parent.width
                    wrapMode: Text.WordWrap

                    text: qsTr("Your login information is only used once to get through the OAuth process.<br>It is not stored within the app or elsewhere.<br>If you don't trust this app, check the 'Use external browser' option to use the standard browser of the OS to login and enter only the PIN in this app.")
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: 18
                }
            }

            SectionEntryInput {
                id: usernameInput
                titleText: qsTr("Username:")
                opacity: appLogin.checked
            }

            SectionEntryInput {
                id: passwordInput
                titleText: qsTr("Password:")
                password: true
                opacity: appLogin.checked
            }

            Item {
                id: startButton
                width: parent.width
                height: childrenRect.height + 12
                Button {
                    anchors.top: parent.top
                    anchors.topMargin: 6
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr("Begin authentification")
                    onClicked: {
                        controlFlickable.visible = false;
                        oauth.beginAuthentication()
                    }
                }
            }


        }
    }
}
